import type { IColumn } from '../typings/index'
/**
 * 默认列表转换
 * @param {Array} columns 
 * @param {Array} fieldsList 
 * @returns 
 */
export function defaultFieldsParser(columns:string[], fieldsList: IColumn[]) {
  // 原始默认列数据，包含了合并列数据
  const originColumnsTitle = columns.slice(0, 1)[0].split('\n')[1]?.split?.(',') || []

  // 处理合并列
  let mergeDefaultColumns:any[] = []
  originColumnsTitle.forEach((item, index) => {
    // 没有+号，作为单列处理
    if (!item.includes('+')) {
      const target = fieldsList.find(i => i.title === item)
      if (target) {
        mergeDefaultColumns.push(target)
      }
      return 
    }

    // 合并列，找到对应的列数据
    const targetList = item.split('+').reduce((prev, cur) => {
      const target = fieldsList.find(i => i.title === cur)
      if (target) {
        prev.push(target)
      }
      return prev
    }, [] as IColumn[])
  

    if (targetList.length) {
      mergeDefaultColumns.push({
        title: '合并列',
        dataIndex: targetList.map(i => i.dataIndex).join(','),
        _type: -1,
        fixed: targetList.some(i => i.fixed) ? 'left' : null,
        width: Math.max.apply(null, targetList.map(i => i.width)),
        _disabled: targetList.some(i => i.fixed),
        _mergeChildren: targetList
      })
    }
  })

  return mergeDefaultColumns
}